import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import Layout from '@/layout'


export const constantRoutes = [


  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/front',
    name: 'Front',
    component: () => import('@/views/front/Front'),
    hidden: true,
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/front/Home.vue')
      },
      {
        path: 'person',
        name: 'appPerson',
        component: () => import('@/views/front/person.vue')
      },
      {
        path: 'studentExam',
        name: 'studentExam',
        component: () => import('@/views/front/studentExam.vue')
      },
      {
        path: 'video',
        name: 'video',
        component: () => import('@/views/front/Video.vue')
      },
      {
        path: 'paper',
        name: 'paper',
        component: () => import('@/views/front/Paper.vue')
      },
      {
        path: 'chat',
        name: 'chat',
        component: () => import('@/views/front/Chat.vue'),
        hidden: true
      },
    ]
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {title: '首页', icon: 'dashboard'}
    },
        {
          path: 'person',
          component: () => import('@/views/person/index'),
          name: 'Person',
          hidden:true,
          meta: {title: '个人中心', icon: 'user'}
        },

      {
        path: 'forget',
        component: () => import('@/views/user/Password'),
        name: 'forget',
        hidden:true,
        meta: {title: '忘记密码', icon: 'user'}
      },
      {
        path: '/handerPaper',
        component: () => import('@/views/studentPaper/handPaper'),
        hidden: true
      },
      {
        path: '/authRedirect',
        component: () => import('@/views/auth-redirect/auth-redirect'),
        hidden: true
      },

    ]
  },

  {path: '*', redirect: '/404', hidden: true}
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}



export const setRoutes = () => {

  const storeMenus = localStorage.getItem("menus");

  if (storeMenus) {

    // 拼装动态路由
    const manageRoute = {
      path: '/',

      component: Layout,
      redirect: "/dashboard",
      children: [] }
    const menus = JSON.parse(storeMenus)

    menus.forEach(item => {
      if (item.path) {  // 当且仅当path不为空的时候才去设置路由
        let itemMenu = { path: item.path.replace("/", ""),
          // component: () => import('@/views/' + item.pagePath + '/index.vue')
          component: (resolve) => require([`@/views/${item.pagePath}`], resolve)

        }
        manageRoute.children.push(itemMenu)
      } else if(item.children.length) {
        item.children.forEach(item => {
          if (item.path) {
            let itemMenu = { path: item.path.replace("/", ""),
              component: (resolve) => require([`@/views/${item.pagePath}`], resolve)}

            // component: () => import('@/views/' + item.pagePath + '/index.vue')}
            manageRoute.children.push(itemMenu)
          }
        })

      }
      router.addRoute(manageRoute)
    })
    // 动态添加到现在的路由对象中去


  }
}

// 重置我就再set一次路由
setRoutes()

router.beforeEach((to, from, next) => {
  console.log("================>to:", to);
  if (to.path === '/authRedirect') {
    console.log("<================>to:", to);
    if (to.query.userid) {
      sessionStorage.setItem("userid", to.query.userid)
    }

    next();
  } else {
    if (to.meta.title) {
      document.title = to.meta.title;
    }
    next();
  }
});

export default router
